package com.xbh.dms.service.impl;

import com.jfinal.aop.AopManager;
import com.jfinal.json.FastJson;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.redis.Redis;
import com.xbh.common.model.Student;
import com.xbh.dms.service.StuService;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;


public class StuServiceImpl implements StuService {

    public static final Student stuDao = new Student().dao();
    public static final Student stuSet = new Student();

    @Override
    public String getOne(String stuId) {

        Record record = Db.findById("tbl_student", "stu_id", Integer.valueOf(stuId));
        String json_ = record.toJson();
        Redis.use("stu").set(record.get("stu_id") + "", json_);
        System.out.println("从mysql中拿到的数据-->" + record.get("stu_id"));
        return json_;
//        return stuDao.findById(stuId);
    }

    @Override
    public List<Student> getAll() {

//        return stuDao.find("select * from tbl_student");
        return stuDao.findAll();
    }

    @Override
    public Page<Student> paginate(int pageNum, int pageSize, int stuId) {

        return stuDao.paginate(pageNum, pageSize, "select *", "from tbl_student where stu_id > ?",stuId);
    }

    @Override
    public boolean addOne(Student student) {
        return stuSet.set("stu_name", student.getStuName()).set("stu_num", student.getStuNum()).save();
    }

    @Override
    public Record addOne0(Student student) {
        Record record = new Record();
        record.set("stu_name", student.getStuName()).set("stu_num", student.getStuNum());
        Db.save("tbl_student", record);
        return record;
    }

    @Override
    public boolean updateOne(Student student) {


        return stuSet.set("stu_id", student.getStuId()).set("stu_name", student.getStuName()).set("stu_num", student.getStuNum()).update();
    }

    @Override
    public boolean deleteById(Integer stuId) {


        return Db.deleteById("tbl_student","stu_id", stuId);
    }

    @Override
    public List<Long> queryInteger() {

        return Db.query("select stu_id from tbl_student");
//        return null;
    }

    @Override
    public List<Student> enjoyDemo(int para0, int para1) {

        Kv kv = Kv.by("stu_id", para0).set("stu_num", para1);

        List<Record> list0 = Db.template("demo1", kv).find();
        Function function = o -> FastJson.getJson().parse(((Record) o).toJson(), Student.class);

        List list = new ArrayList<Student>();

        for (Record record : list0) {
            list.add(function.apply(record));
        }
        return list;

//        SqlPara sqlPara = Db.getSqlPara("demo0", para0, para1);
//        return stuDao.find(sqlPara);
    }


    @Override
    public String beachTest(List<Student> list) throws Exception {

        /*int[] result = Db.batchSave(list, list.size());
        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);
        }*/
        for (Student student : list) {
            if(student.getStuId() == 16) {
                System.out.println(111111);
//                System.out.println(1/0);
            }
            System.out.println("操作" + student.getStuId() + "中-->>>");
            stuSet.set("stu_id", student.getStuId()).set("stu_name", student.getStuName()).set("stu_num", student.getStuNum()).update();
            System.out.println(student.getStuId() + "操作"+ "完毕-->>>");
        }
        return "Y";
    }


}
